﻿#include "record_dialog.h"
#include "ui_record_dialog.h"
#include "basal_sql.h"
#include <QCompleter>
#include <QComboBox>
#include <QDebug>
#include <QDate>
//收银员记录打印
record_dialog::record_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::record_dialog)
{
    ui->setupUi(this);

    //参考produce_dialog.cpp中所写
    QStringList str;
    QSqlQuery query;
        query.exec(tr("select username from user"));
        while(query.next())
        {
            QString username = query.value(0).toString();
            str.append(username);
        }
        QCompleter* completer = new QCompleter(str, this);
        ui->userBox->clear();
        ui->userBox->addItems(str);
        ui->userBox->setCompleter(completer);

}

record_dialog::~record_dialog()
{
    delete ui;
}


void record_dialog::on_showCashBtn_clicked()
{
    //获取dealing表中收银员的信息 直接使用字符串来输出信息
    QSqlQuery query;
    QString user = ui->userBox->currentText();
    QString outText = "收银员" +user+ "的收银记录" + "\n";
    QString sql = "select * from dealing where username='" +user+ "'";
    query.exec(sql);
    // qDebug()<<"ss";
    while(query.next()){//循环输出
        QString id = query.value(0).toString();
        QString price = query.value(1).toString();
        QString date = query.value(3).toString();
        outText.append("\n收银编号：" +id+ "\n收取金额：" +price+ "\n收银时间" +date);
        outText.append("\n--------------------------");
        ui->tEditCash->setText(outText);  //调整
    }
}

void record_dialog::on_showWeekBtn_clicked()
{
    //获取dealing表中周记录信息
    QString outText;
    QSqlQuery query;
    // QDateTime time;
    outText = "前七天的记录\n";
    //获取当前时间减去7来获取信息
    QDate currentDate = QDate::currentDate();
    qint64 currentDay = currentDate.toJulianDay();
    for(qint64 diff = 0;diff < 7; diff++) {
        QDate tmpDate = QDate::fromJulianDay(currentDay-diff);
        QString currentDateStr = tmpDate.toString("yyyy年MM月dd日");
        QString sql = "select * from dealing where p_date like '" +currentDateStr+ "%'";
        query.exec(sql);
        while(query.next()){
            // qDebug()<<"ss";
            QString id = query.value(0).toString();
            QString price = query.value(1).toString();
            QString username = query.value(2).toString();
            QString date = query.value(3).toString();
            outText.append("\n收银编号：" +id+ "\n收银员姓名：" +username+ "\n收取金额：" +price+ "\n收银时间" +date);
            outText.append("\n------下一天的记录-------");
        }
    }
    ui->tEditWeek->setText(outText);
}
